/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package service;

import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 *
 * @author ambaye
 */
public class EmailService {
    private static String msg = "";
    private static String user = "katior@hotmail.com";
    private static String pwd = "abc123...";
    private static String from = "lunembaye@gmail.com";
    private static String to = "";
    private static String sujet = "Coucou";
    private static String messageToSend = ""
            + "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>\r\n"
            + "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\r\n"
            + "<html xmlns=\"http://www.w3.org/1999/xhtml\">\r\n"
            + "<head>\r\n"
            + "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\" />\r\n"
            + "<title>Votre Facture</title>\r\n"
            + "</head>\r\n"
            + "<body>\r\n"
            + msg + "\r\n"
            + "</body>\r\n" + "</html>";

    public static void sendMail(String emailTo, String messageToSends) {
//        to = emailTo;
//        msg = messageToSends;
        
        Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.socketFactory.class",
                "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", "465");

        Session session = Session.getInstance(props,
                new javax.mail.Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
						// Adresse mail et password de l emetteur sur le serveur
                        // smtp
                        return new PasswordAuthentication(user, pwd);
                    }
                });

        try {
            Message message = new MimeMessage(session);
            // adresse mail de l emetteur
            message.setFrom(new InternetAddress(from));
            // adresse destinataire
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(emailTo));
            message.setSubject(sujet);
            message.setContent(messageToSends, "text/html; charset=utf-8");

            Transport.send(message);

        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }
    }
    
}
